package cn.com.connext.scrm.service;

import cn.com.connext.scrm.domain.enumeration.AuditStatus;
import cn.com.connext.scrm.domain.enumeration.DeleteStatus;
import cn.com.connext.scrm.domain.master.*;
import cn.com.connext.scrm.repository.master.*;
import cn.com.connext.scrm.service.dto.*;
import cn.com.connext.scrm.web.rest.vm.*;
import com.alibaba.fastjson.JSON;
import com.connext.common.exception.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.Predicate;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.time.ZonedDateTime;
import java.util.*;

/***
 *  created by cuijian on 2021/05/11
 *
 *
 */
@Service
@Transactional
public class QrCodeService extends BaseService<QrCode, String> {
    private final Logger log = LoggerFactory.getLogger(QrCodeService.class);

    @Autowired
    private QrCodeRepository qrCodeRepository;
    @Autowired
    private QrCodeScanRepository qrCodeScanRepository;


    public QrCodeService(QrCodeRepository qrCodeRepository) {
        super(qrCodeRepository);
        this.qrCodeRepository = qrCodeRepository;
    }



    public Page<QrCodeDTO> queryDrawList(QueryQrCodeVM vm) {
        Pageable pageable = new PageRequest(vm.getPage(), vm.getPageSize(), new Sort(Sort.Direction.DESC, "createTime"));
        Specification<QrCode> specification = (root, query, criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();
            if (StringUtils.isNotEmpty(vm.getSceneName())) {
                list.add(criteriaBuilder.like(root.get("sceneName"), "%"+vm.getSceneName()+"%"));
            }
            if (StringUtils.isNotEmpty(vm.getAssociatedKeyword())) {
                list.add(criteriaBuilder.like(root.get("associatedKeyword"), "%"+vm.getAssociatedKeyword()+"%"));
            }
            if (StringUtils.isNotEmpty(vm.getQrCodeType())) {
                list.add(criteriaBuilder.equal(root.get("qrCodeType"), vm.getQrCodeType()));
            }
            if (vm.getStartTime()!=null&&vm.getEndTime()!=null) {
                list.add(criteriaBuilder.between(root.get("createTime"), vm.getStartTime(), vm.getEndTime()));
            }
            if(vm.getStartTime()!=null&&vm.getEndTime()==null){
                list.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createTime"),vm.getStartTime()));
            }
            if(vm.getStartTime()==null&&vm.getEndTime()!=null){
                list.add(criteriaBuilder.lessThanOrEqualTo(root.get("createTime"),vm.getEndTime()));
            }




            return criteriaBuilder.and(list.toArray(new Predicate[0]));
        };
        Page<QrCode> page = qrCodeRepository.findAll(specification, pageable);
        Page<QrCodeDTO> dtoPage = page.map(qrcode -> this.toQrCodeDTO(qrcode));
        return dtoPage;
    }

    private QrCodeDTO toQrCodeDTO(QrCode activity) {
        QrCodeDTO dto = new QrCodeDTO();
        BeanUtils.copyProperties(activity, dto);
        return dto;
    }
    public QrCodeDTO add(QrCodeListVM vm) {

        QrCode dto = new QrCode();
        BeanUtils.copyProperties(vm, dto);
        QrCodeDTO qrCodeDTO = new QrCodeDTO();
        BeanUtils.copyProperties(qrCodeRepository.saveAndFlush(dto),qrCodeDTO);
        return   qrCodeDTO;
    }
    public QrCodeDTO update(QrCodeListVM vm) {
        QrCode dto = qrCodeRepository.findOne(vm.getId());
        dto.setQrCodeState(vm.getQrCodeState());
        QrCodeDTO qrCodeDTO = new QrCodeDTO();
        BeanUtils.copyProperties(qrCodeRepository.saveAndFlush(dto), qrCodeDTO);
        return qrCodeDTO;

    }
    public QrCodeDTO selectByid(QrCodeListVM vm) {
        QrCode dto = qrCodeRepository.findOne(vm.getId());
        QrCodeDTO qrCodeDTO = new QrCodeDTO();
        BeanUtils.copyProperties(qrCodeRepository.saveAndFlush(dto), qrCodeDTO);
        return qrCodeDTO;

    }

    public QrCodeScanDTO addScan(QrCodeScanVM vm) {
        QrCodeScan dtoscan = new QrCodeScan();
        BeanUtils.copyProperties(vm, dtoscan);
        QrCodeScanDTO qrCodeDTOscan = new QrCodeScanDTO();
        BeanUtils.copyProperties(qrCodeScanRepository.saveAndFlush(dtoscan),qrCodeDTOscan);
        //次数加一
        QrCode dto = qrCodeRepository.findOne(vm.getQrCodeId());
        int count=dto.getScanningVolume()+1;
        dto.setScanningVolume(count);
        qrCodeRepository.saveAndFlush(dto);
        return   qrCodeDTOscan;
    }
    public boolean addQrCodeCheck(QrCodeListVM vm) {
        Specification<QrCode> specification = (root, query, criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();
            if (StringUtils.isNotEmpty(vm.getSceneName())) {
                list.add(criteriaBuilder.equal(root.get("sceneName"), vm.getSceneName()));
            }
            if (StringUtils.isNotEmpty(vm.getAssociatedKeyword())) {
                list.add(criteriaBuilder.equal(root.get("associatedKeyword"), vm.getAssociatedKeyword()));
            }
            if (StringUtils.isNotEmpty(vm.getQrCodeType())) {
                list.add(criteriaBuilder.equal(root.get("qrCodeType"), vm.getQrCodeType()));
            }
            return criteriaBuilder.and(list.toArray(new Predicate[0]));
        };

        if(qrCodeRepository.findOne(specification)==null){
            return  true;

        }
        else{
            return false;
        }


    }

    public List<QrCodeDTO> queryQrCodeBySceneName(QrCodeListVM vm) {
        if (StringUtils.isEmpty(vm.getSceneName())) {
            List<QrCode> qrCodes = qrCodeRepository.findAll();
            List<QrCodeDTO> qrCodeDTOStream = qrCodes.stream().map(qrCode -> this.toQrCodeDTO(qrCode)).collect(Collectors.toList());
            return qrCodeDTOStream;
        } else {
            List<QrCode> qrCodes = qrCodeRepository.queryQrCodesBySceneName(vm.getSceneName());
            List<QrCodeDTO> qrCodeDTOStream = qrCodes.stream().map(qrCode -> this.toQrCodeDTO(qrCode)).collect(Collectors.toList());
            return qrCodeDTOStream;
        }
    }
}
